package com.ruoyi.app.controller;

import com.ruoyi.app.domain.ExamProject;
import com.ruoyi.app.domain.ExamProjectExamintions;
import com.ruoyi.app.domain.Examintions;
import com.ruoyi.app.service.ExamProjectExamintionsService;
import com.ruoyi.app.service.ExamProjectService;
import com.ruoyi.app.service.ExamintionsService;
import com.ruoyi.app.util.Result;
import com.ruoyi.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RequestMapping("project")
@RestController
public class PojectController {
    @Autowired
    ExamProjectService examProjectService;
    @Autowired
    ExamProjectExamintionsService projectExamintionsService;
    @Autowired
    ExamintionsService examintionsService;
    @Autowired
    RedisService redisService;
    @RequestMapping("list")
    public Result list(Integer id) {
        Boolean aBoolean = redisService.hasKey("project" + id);
        if(aBoolean == true){
            return Result.ok(redisService.getCacheList("project"+id));
        }
        List<ExamProject> examProjects = examProjectService.list(id);
        for (ExamProject examProject : examProjects) {
            List<ExamProjectExamintions> examProjectExamintions = projectExamintionsService.getExamProjectExamintions(examProject.getId());
            for (ExamProjectExamintions examProjectExamintion : examProjectExamintions) {
                examProjectExamintion.setExamintionsName(examintionsService.getById(examProjectExamintion.getExamintionsId()).getExName());
            }
            examProject.setExamProjectExamintions(examProjectExamintions);
        }
        redisService.setCacheList("project"+id,examProjects);
        return Result.ok(examProjects);
    }

}
